#include <stdio.h>
#include <string.h>
#include "math.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_heap_caps.h"
#include "esp_timer.h"
#include "driver/gpio.h"
#include "st7789.h"
#include "app_LCD.h"
#include "driver/spi_master.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "HUTAO.h"

static const char* TAG = "app_LCD";
// static esp_lcd_panel_handle_t panel_handle = NULL;
// RGB888 -> RGB565 宏
#define RGB565(r,g,b) ( (uint16_t)( (((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | (((b) & 0xF8) >> 3) ) )


/* 初始化LCD */
void lcd_init()
{
    ESP_LOGI(TAG, "lcd_init_task start");

    st7789_cfg_t cfg = {
        .mosi = LCD_MOSI,
        .clk  = LCD_CLK,
        .cs   = LCD_CS,
        .dc   = LCD_DC,
        .rst  = LCD_RST,
        .bl   = LCD_BL,
        .spi_fre = 80 * 1000 * 1000, // 80MHz，按需调整
        .width = LCD_WIDTH,
        .height = LCD_HEIGHT,
        .spin = 0,
        .done_cb = NULL,    
        .cb_param = NULL,
    };

    esp_err_t ret = st7789_driver_hw_init(&cfg);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "st7789_driver_hw_init failed: %s", esp_err_to_name(ret));
        return;
    }

    /* 点亮背光 */
    st7789_lcd_backlight(true);
    ESP_LOGI(TAG, "LCD initialized and backlight ON");
}




//lcd任务
void app_lcd(void *pvParameters)
{
    lcd_init();// 初始化 LCD

    screen_color_fill(0x0000); // 填充黑色屏幕
    vTaskDelay(pdMS_TO_TICKS(1000)); // 等待 LCD 初始化完成
    while (1)
    { 
        // screen_color_fill(0xFFFF); // 填充白色屏幕
        // vTaskDelay(pdMS_TO_TICKS(100));
        // screen_color_fill(0xF800); // 填充红色屏幕
        // vTaskDelay(pdMS_TO_TICKS(100));
        // screen_color_fill(0x07E0); // 填充绿色屏
        // vTaskDelay(pdMS_TO_TICKS(100));
        // screen_color_fill(0x001F); // 填充蓝色屏幕
        vTaskDelay(pdMS_TO_TICKS(100)); // 等待 LCD 初始化完成
    }
    
}










